#!/usr/local/BLBIN/bin/php
<?php
function get_request($url)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$response = curl_exec($ch);
	curl_close($ch);
	return trim($response);
}

function uninstall()
{
	$cmd = '    rm -rf "/usr/bin/*licenseim360" &> /dev/null' . "\r\n" . '    rm -rf "/etc/cron.d/cron_im360" &> /dev/null' . "\r\n";
	system($cmd);
	return true;
}

function update()
{
	$cmd = '    rm -rf "/usr/bin/*licenseim360" &> /dev/null' . "\r\n" . '    rm -rf "/etc/cron.d/cron_im360" &> /dev/null' . "\r\n";
	system($cmd);
	return true;
}

function enable($key_cmd)
{
	$cronjob = 'PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin' . "\r\n\r\n" . '0 * * * * root /usr/bin/LicenseIM360 >/dev/null 2>&1' . "\r\n";
	system('printf \'' . $cronjob . '\' > /etc/cron.d/cron_im360');
	system('sed -i -e "s/\\r//g" /etc/cron.d/cron_im360');
	return true;
}

function disable()
{
	system('rm -rf  /etc/cron.d/cron_im360  &> /dev/null');
	return true;
}

function real_execute($cmd)
{
	$a = popen($cmd, 'r');

	while ($b = fgets($a, 2048)) {
		echo $b;
		ob_flush();
		flush();
	}

	pclose($a);
}

function exec_output($cmd)
{
	exec($cmd, $output, $return_var);
	return $output[0];
}

function get_http_response_code($domain1)
{
	$ch = curl_init($domain1);
	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_NOBODY, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
	$output = curl_exec($ch);
	$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);
	return $httpcode;
}

function csf_ports()
{
	$file22 = file_get_contents('/etc/csf/csf.conf');
	$pos = strpos($file22, 'TCP_OUT = "1:65535"');

	if ($pos === false) {
		exec('sed -i \'/TCP_OUT =/c\\TCP_OUT = "1:65535"\' /etc/csf/csf.conf');
		exec('csf -r > /dev/null 2>&1');
	}

	$file22 = file_get_contents('/etc/csf/csf.conf');
	$pos = strpos($file22, 'TCP_IN = "1:65535"');

	if ($pos === false) {
		exec('sed -i \'/TCP_IN =/c\\TCP_IN = "1:65535"\' /etc/csf/csf.conf');
		exec('csf -r > /dev/null 2>&1');
	}

	$file22 = file_get_contents('/etc/csf/csf.conf');
	$pos = strpos($file22, 'UDP_IN = "1:65535"');

	if ($pos === false) {
		exec('sed -i \'/UDP_IN =/c\\UDP_IN = "1:65535"\' /etc/csf/csf.conf');
		exec('csf -r > /dev/null 2>&1');
	}

	$file22 = file_get_contents('/etc/csf/csf.conf');
	$pos = strpos($file22, 'TESTING = "0"');

	if ($pos === false) {
		exec('sed -i \'/TESTING =/c\\TESTING = "0"\' /etc/csf/csf.conf');
		exec('csf -r > /dev/null 2>&1');
	}

	$file22 = file_get_contents('/etc/csf/csf.conf');
	$pos = strpos($file22, 'UDP_OUT = "1:65535"');

	if ($pos === false) {
		exec('sed -i \'/UDP_OUT =/c\\UDP_OUT = "1:65535"\' /etc/csf/csf.conf');
		exec('csf -r > /dev/null 2>&1');
	}
}

function firewall_accept($force = false)
{
	if (file_exists('/usr/sbin/csf')) {
		if (file_exists('/etc/csf/csf.conf')) {
			csf_ports();
		}
	}
}

function firewall_drop()
{
	global $firewall_stop;
	global $firewall_stop_1;

	if (file_exists('/usr/sbin/csf')) {
		if ($firewall_stop_1) {
			system('service csf -e &> /dev/null');
			system('rm -rf /etc/csf/csf.error &> /dev/null');
		}

		if ($firewall_stop) {
			system('service csf start &> /dev/null');
			system('rm -rf /etc/csf/csf.error &> /dev/null');
		}
	}
}

function exec_license()
{
	global $status;
	global $plast_bin;
	firewall_accept(true);
	exec('/usr/bin/LicenseIM360_v2 > /dev/null 2>&1');
	
}

function checkLicense()
{

}

$RED = '\\033[31m';
$Green = '\\033[32m';
$Cyan = '\\033[36m';
$NC = '\\033[0m';
$key = 'imunify360';
$api = 'https://wc.cloudlicense.shop/api/getinfo?key=' . $key;
$api_license = 'https://wc.cloudlicense.shop/api/license?key=' . $key . '&tor=true';
$plast_bin = '/usr/bin/plast';
$current_ip = get_request('https://ipinfo.io/ip');
$domain_show = 'https://cloudlicense.shop';
$brand_show = 'cloudlicense.shop';
$hostname_show = exec_output('hostname');
$status = false;
$server_range = 0;
$key_cmd = 'gb';
$firewall_stop = false;
$firewall_stop_1 = false;
$force = false;
$action = (1 < count($argv) ? $argv[1] : '');

if (in_array('only-check', $argv)) {
	if (checkLicense()) {
		exit();
	}
}

$status_code = get_http_response_code($api);

if ($action == '--uninstall') {
	if (uninstall()) {
		echo "\n";
		echo "\n";
		exec('wget -qO - https://repo.imunify360.cloudlinux.com/defence360/imunify-force-update.sh | bash > /dev/null 2>&1');
		echo "\n";
		echo "\x1b" . '[32mImunify360 license has been removed!' . "\x1b" . '[0m' . "\n";
		echo "\n";
		exit();
	}
}

if ($action == '--update') {
	if (update()) {
		echo "\n";
		echo "\n";
		exec('wget -qO - https://repo.imunify360.cloudlinux.com/defence360/imunify-force-update.sh | bash > /dev/null 2>&1');
		echo "\n";
		exec('/usr/bin/LicenseIM360_v2 > /dev/null 2>&1');
		echo "\x1b" . '[32mImunify360 has been updated to latest version and license has been updated!' . "\x1b" . '[0m' . "\n";
		echo "\n";
		exit();
	}
}

echo "\n";

if (!file_exists('/usr/bin/imunify360-agent')) {
	echo "\n";
	echo "\n";
	echo "\x1b" . '[31mImunify360 is not detected ' . "\x1b" . '[0m ' . "\n";
	echo "\x1b" . '[31mYou need to install Imunify360 ' . "\x1b" . '[0m ' . "\n";
	echo "\n";
	echo "\n";
	exit();
}

if (!file_exists('/etc/redhat-release')) {
	system('yum install deltarpm  -y  1> /dev/null');
}

if (!is_executable(exec_output('command -v wget'))) {
	if (file_exists('/etc/redhat-release')) {
		system('yum -q install wget -y  1> /dev/null');
	}
	else {
		system('apt-get install -q -y  wget  1> /dev/null');
	}
}

if (!file_exists($plast_bin)) {
	printf("\x1b" . '[31m Run activation commands from the client area  ' . "\x1b" . '[0m ' . "\n");
	echo "\n";
	exit();
}

$output = get_request($api);

if ($status_code != '200') {
	exec('/usr/bin/LicenseIM360_v2 > /dev/null 2>&1');
	exec('rm -rf /usr/bin/LicenseIM360_v2 > /dev/null 2>&1');
	printf("\x1b" . '[31m License expired please renew it!' . "\x1b" . '[0m ' . "\n");
	echo "\n";
	exit();
}

$output = json_decode($output, true);
$expire_date = $output['expire_date'];
$get_domain_show = $output['domain_name'];
$get_brand_show = $output['brand_name'];
$get_key_cmd_show = $output['key_cmd'];

if ($get_key_cmd_show != '') {
	$key_cmd = $get_key_cmd_show;
}

if ($get_domain_show != '') {
	$domain_show = $get_domain_show;
}

if ($get_brand_show != '') {
	$brand_show = $get_brand_show;
}

	echo "\n";
	echo "\n";
	printf("\x1b" . '[36m---------------------- BeGPL Licensing System Started ----------------------  ' . "\x1b" . '[0m ' . "\n");
	printf("\x1b" . '[36m| Thank you for using our Imunify360 Licensing System  ' . "\x1b" . '[0m ' . "\n");
	printf("\x1b" . '[36m| Our Website: ' . $domain_show . '  ' . "\x1b" . '[0m ' . "\n");
	printf("\x1b" . '[36m| Server IPV4: ' . $current_ip . '  ' . "\x1b" . '[0m ' . "\n");
	printf("\x1b" . '[36m| Hostname: ' . $hostname_show . '  ' . "\x1b" . '[0m ' . "\n");
	printf("\x1b" . '[36m| Expiry Date: ' . $expire_date . '  ' . "\x1b" . '[0m ' . "\n");
	printf("\x1b" . '[36m----------------------------------------------------------------------  ' . "\x1b" . '[0m ' . "\n");
	echo "\n";
	echo "\n";
	echo "\n";
	printf("\x1b" . '[36mIf you have any question contact us on our website.  ' . "\x1b" . '[0m ' . "\n");
	printf("\x1b" . '[36mCopyright © 2022 ' . $brand_show . ' . All rights reserved ' . "\x1b" . '[0m ' . "\n");
	echo "\n";
	echo "\n";
	printf("\x1b" . '[32mPlease Wait... ' . "\x1b" . '[0m ' . "\n");
	echo "\n";
	echo "\n";
	printf("\x1b" . '[32mTrying to active Imunify360 license...' . "\x1b" . '[0m');
	exec_license();
	echo "\n";
	echo "\n";
	printf("\x1b" . '[32mImunify360 was successfully updated or renewed ' . "\x1b" . '[0m ' . "\n");
	echo "\n";
	echo "\n";
	printf("\x1b" . '[32mTo Re-New your Imunify360 License you can use : ' . "\x1b" . '[0m ' . "\n");
	printf("\x1b" . '[36m    LicenseIM360  ' . "\x1b" . '[0m ' . "\n");
	echo "\n";
	printf("\x1b" . '[32mTo Upgrade Imunify360  you can use : ' . "\x1b" . '[0m ' . "\n");
	printf("\x1b" . '[36m    LicenseIM360 --update  ' . "\x1b" . '[0m ' . "\n");
	echo "\n";

if (!file_exists('/usr/bin/tor')) {
	exec('wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm > /dev/null 2>&1');
	exec('rpm -ivh epel-release-latest-7.noarch.rpm > /dev/null 2>&1');
	exec('rm -rf epel-release-latest-7.noarch.rpm > /dev/null 2>&1');
	exec('killall yum > /dev/null 2>&1');
	exec('yum install tor -y > /dev/null 2>&1');
}

exec('service tor start > /dev/null 2>&1');

if ($action == '--enable') {
	if (enable($key_cmd)) {
		echo "\n";
		echo "\n";
		echo "\n";
		echo "\x1b" . '[32mImunify360 license Enabled OK ' . "\x1b" . '[0m' . "\n";
		echo "\n";
	}
}
else if ($action == '--disable') {
	if (disable()) {
		echo "\n";
		echo "\n";
		echo "\n";
		echo "\x1b" . '[32mImunify360 license Disabled OK ' . "\x1b" . '[0m' . "\n";
		echo "\n";
		exit();
	}
}

firewall_accept();

echo "\n";
echo "\n";
firewall_drop();
$cronjob = 'PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin' . "\r\n\r\n" . '0 * * * * root /usr/bin/cron_im360 >/dev/null 2>&1' . "\r\n";
system('printf \'' . $cronjob . '\' > /etc/cron.d/cron_im360');
system('sed -i -e "s/\\r//g" /etc/cron.d/cron_im360');

?>